cc.Class({
    extends: cc.Component,

    properties: {
        pickRadius: 0,
        game : {
            default: null,
            type: cc.Node,
            serializable: false 
        }
    },

    getPlayerDistance: function () {
        // 根据 player 节点位置判断距离
        var playerPos = Global.playerSelf.node.getPosition();
        // 根据两点位置计算两点之间距离
        var dist = cc.pDistance(this.node.position, playerPos);
        return dist;
    },

    onPicked: function() {
        // 当星星被收集时，生成一个新的星星
        if (Global.gameover == true) {
            return;
        }
        var sendData = {
            type : "gainScore",
            starid: Global.starid
        };
        var sendString = JSON.stringify(sendData);
        Global.ws.send(sendString);
        //加分
        this.game.gainScore(1);
        // 然后销毁当前星星节点
        this.node.destroy();

    },
    update: function (dt) {
        // 每帧判断和主角之间的距离是否小于收集距离
        if (this.getPlayerDistance() < this.pickRadius) {
            // 调用收集行为
            this.onPicked();
            return;
        }
        this.node.opacity -= 255 * dt  / Global.game.starDuration;

        if (this.node.opacity < 0) {
            this.node.opacity = 0;
            //星星到时间消失 只由房主通知服务器去生成
            if (Global.isJoin == false) {
                this.getNewStar();
            }
            this.node.destroy();
        }
    },
    getNewStar: function() {
        if (Global.gameover == true) {
            return;
        }
        var sendData = {
                type: "getNewStar"
            };
        var sendString = JSON.stringify(sendData);
        Global.ws.send(sendString);
    }


});
